package com.scau.action;

import java.util.List;

import net.sf.json.JSONArray;

import com.scau.dao.IProductDao;
import com.scau.service.IProductService;
import com.scau.vo.Product;

/**
 * @author Jinglun
 * @create 2017年4月18日
 */
public class ProductAction extends SuperAction {
	private String result;// 属性result，用于异步传输数据交互
	
	private Product product;
	private IProductService productService;
	
	// save
	public String saveProduct() {
		Product p = getProduct();
		System.out.println(p);
		if (productService.saveProduct(p)) {
			// 查出所以product，返回json
			List<Product> proList = productService.listProduct();
			JSONArray jsonArray = JSONArray.fromObject(proList.toString());
			result = jsonArray.toString();
			return "success";
		} else
			return "error";
	}
	
	// list
	public String listProduct() {
		List<Product> pList = productService.listProduct();
		JSONArray jsonArray = JSONArray.fromObject(pList.toString());
		result = jsonArray.toString();
		return "success";
	}
	
	// delete
	public String deleteProduct() {
		Product p = getProduct();
		if (productService.deleteProduct(p.getId())) {
			List<Product> pList = productService.listProduct();
			JSONArray jsonArray = JSONArray.fromObject(pList.toString());
			result = jsonArray.toString();
			return "success";
		} else
			return "error";
	}
	
	// update
	public String updateProduct() {
		Product p = getProduct();
		if (productService.updateProduct(p)) {
			List<Product> pList = productService.listProduct();
			JSONArray jsonArray = JSONArray.fromObject(pList.toString());
			result = jsonArray.toString();
			return "success";
		} else
			return "error";
	}

	// get,set方法
	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}

	public Product getProduct() {
		return product;
	}

	public void setProduct(Product product) {
		this.product = product;
	}

	public IProductService getProductService() {
		return productService;
	}

	public void setProductService(IProductService productService) {
		this.productService = productService;
	}
	
	
}
